package dao;

import hbt.HibernateUtil;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import entidades.Cliente;



public class ClienteDAO {
	
	private static ClienteDAO instancia = null;
	private static SessionFactory sf = null;
	
	public static ClienteDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new ClienteDAO();
		} 
		return instancia;
	}
	
	public void guardarUnCliente(Cliente fa){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(fa); 
		session.getTransaction().commit();
		session.close();
	}
	
	@SuppressWarnings("unchecked")
	public List<Cliente> obtenerListaClientes() throws Exception {
		try {
			Session session = sf.openSession();
			Query q = session.createQuery("from Cliente");
			List<Cliente> listaRubros = new ArrayList<Cliente>(q.list());
			session.close();
			return listaRubros;
		} catch (Exception e) {
			throw e;
		}
	}
	
	@SuppressWarnings("unchecked")
	public Cliente obtenerClienteAlAzar() throws Exception {
		try {
			Session session = sf.openSession();
			Query q = session.createQuery("from Cliente order by newid()");
			List<Cliente> listaClientes = new ArrayList<Cliente>(q.list());
			session.close();
			return listaClientes.get(0);
		} catch (Exception e) {
			throw e;
		}
	}
}
